package com.example.pattern.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * 具体主题
 *
 * @author FM_南风
 * @date 2024/7/6
 */
public class ConcreteSubject implements Subject{
	private List<Observer> observers = new ArrayList<>();
	private int state; // 主题状态

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
		notifyObservers(); // 状态变化时通知所有观察者
	}

	@Override
	public void registerObserver(Observer observer) {
		observers.add(observer);
	}

	@Override
	public void removeObserver(Observer observer) {
		observers.remove(observer);
	}

	@Override
	public void notifyObservers() {
		for (Observer observer : observers) {
			observer.update(state);
		}
	}
}
